package com.bvgol.example.rabbitmq.controller;
import java.math.BigDecimal;

import com.bvgol.example.rabbitmq.entity.OrderEntity;
import com.bvgol.example.rabbitmq.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import sun.util.resources.LocaleData;

import java.time.LocalDate;
import java.util.Date;
import java.util.UUID;

/**
 * @program: my-spring-completely
 * @description:
 * @author: GUOCHEN
 * @create: 2023/03/16 17:11
 */
@RestController
public class RabbitController {

    @Autowired
    RabbitTemplate rabbitTemplate;
    /**
     * @Classname RabbitController
     * @param
     * @return
     * @Description TODO
     * @Date 2023/3/16 17:12
     * @auther by GUOCHEN
     */
     @GetMapping("get/{id}")
     public Object get(@PathVariable("id") Integer id){
         for (Integer i = 0; i < id; i++) {
             if (i%2==0){
                 OrderReturnReasonEntity reasonEntity = new OrderReturnReasonEntity();
                 reasonEntity.setId(i.longValue());
                 reasonEntity.setCreateTime(new Date());
                 reasonEntity.setName("reason");
                 reasonEntity.setStatus(i);
                 reasonEntity.setSort(i);
                 rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java",
                         reasonEntity, new CorrelationData(UUID.randomUUID().toString()));
                 //业务中第四个参数可以传具体实体id
             }else {
                 OrderEntity orderEntity = new OrderEntity();
                 orderEntity.setId(i.longValue());
                 orderEntity.setMemberId(i.longValue());
                 orderEntity.setOrderSn("--"+i);
                 orderEntity.setCreateTime(new Date());
                 rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java",
                         orderEntity, new CorrelationData(UUID.randomUUID().toString()));
             }
         }
        return id;
     }

     /**
      * @Classname RabbitController
      * @param
      * @return
      * @Description TODO
      * @Date 2023/3/17 15:43
      * @auther by GUOCHEN
      */
      @GetMapping("returnCallback/{id}")
      public Object get(@PathVariable("id") String id){
          String s = UUID.randomUUID().toString();
          OrderEntity orderEntity = new OrderEntity();
          orderEntity.setOrderSn(s);
          orderEntity.setBillHeader(id);
          orderEntity.setCreateTime(new Date());
          //这个routingKey 不存在会走收到异常
          rabbitTemplate.convertAndSend("hello-java-exchange", "hello2.java2",
                  orderEntity, new CorrelationData(s));
         return id;
      }

      /**
       * @Classname RabbitController
       * @param
       * @return
       * @Description TODO
       * @Date 2023/3/20 17:45
       * @auther by GUOCHEN
       */
       @GetMapping("creat/{id}")
       public Object creatOrder(@PathVariable("id") String id){
           String s = UUID.randomUUID().toString();
           OrderEntity orderEntity = new OrderEntity();
           orderEntity.setOrderSn(s);
           orderEntity.setBillHeader(id);
           orderEntity.setCreateTime(new Date());
           rabbitTemplate.convertAndSend("order-event-exchange", "order.create.order",
                   orderEntity, new CorrelationData(s));
          return id +":::"+LocalDate.now();
       }
}
